package com.lylg.picture_share.controller;

import com.lylg.picture_share.entity.Image;
import com.lylg.picture_share.entity.Manager;
import com.lylg.picture_share.entity.User;
import com.lylg.picture_share.service.ImageService;
import com.lylg.picture_share.service.ImageTypeService;
import com.lylg.picture_share.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller

public class FrontUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ImageTypeService imageTypeService;
    @Autowired
    private ImageService imageService;
    @RequestMapping("/front/login")
    public String doLogin(String user_name,String user_pwd, HttpSession session){

        User loginUser=userService.selectByNameAndPwd(user_name,user_pwd);


        if(loginUser!= null){
            session.setAttribute("loginuser",loginUser);
            return "redirect:/front/index";
        }
        else{
            return "redirect:/front/index";

        }

    }
    @RequestMapping("/front/userCenterUI")
    public String goUserCenter(){


        return "/front/userCenter.html";

    }

    @RequestMapping("/front/addImageUI")
    public String goAddUI(Map<String,Object> model){

        model.put("types",imageTypeService.selectAllImageType());

        return "/front/addImage.html";


    }

    @RequestMapping("/front/exit")
    public String doExit(HttpSession session){
        //从session中移除
        session.removeAttribute("loginuser");
        return "redirect:/front/index";

    }
    @RequestMapping("/front/saveImage")
    public String saveImage(@RequestParam("image_file") MultipartFile multipartFile , Image image, HttpSession session){


        //得到上传的文件的文件名称（1.mp4）
        String fileName = multipartFile.getOriginalFilename();
        //获取文件后缀名(.mp4)
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名  拼接新文件前缀名和后缀名
        String newFileName = UUID.randomUUID()+suffixName;
        //以上三行代码得到了更改之后的新文件名称

        //获得静态资源的一个路径(/target/classes/static/upvideo)
        String filePath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/upimage";
        File dir=new File(filePath);
        //路径指定的目录((/target/classes/static/upvideo))不存在，就创建目录
        if(!dir.exists()){
            dir.mkdir();
        }
        //在指定目录下根据新文件的名称创建了一个file类型的对象
        File dest = new File(dir , newFileName);
        System.out.println(dest.getAbsolutePath());

        try {
            //将上传上来的文件按照新文件名称存放到指定的路径下
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将上传文件的url要保存到数据库中
        image.setImage_url("upimage/"+newFileName);
        image.setImage_good(0);
        image.setImage_play(0);
        image.setImage_status("未审核");
        image.setImage_time(new Date());

        //设置发布人
        Integer userId=0;
        if(session.getAttribute("loginuser")!=null) {
            userId = ((User) session.getAttribute("loginuser")).getUser_id();
            image.setUser_id(userId);
        }
        imageService.add(image);



        return "redirect:/front/addImageUI";


    }
    @RequestMapping("/front/upduserpwdui")
    public String doUpdPwdUI(){

        return "/front/updPwd.html";

    }
    @RequestMapping("/front/updpwd")
    public String doUpdPwd(String user_pwd,HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null){
            userId=((User)session.getAttribute("loginuser")).getUser_id();
        }

        //修改密码
        userService.updPwd(userId,user_pwd);

        //清除当前的登录状态，让用户重新登录
        session.removeAttribute("loginuser");
        return "redirect:/front/index";

    }
    @RequestMapping("/front/getalluser")
    public String getAlluser(Model model, HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null){
            userId=((User)session.getAttribute("loginuser")).getUser_id();
        }
        User users=userService.selectUserById(userId);
        model.addAttribute("userList",users);
        return "/front/myData.html";

    }
    @RequestMapping("/front/myUploadUI")
    public String goMyUploadUI(Map<String,Object> model, HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null) {
            userId = ((User) session.getAttribute("loginuser")).getUser_id();
        }
        List<Image> images=imageService.getMyUploadImages(userId);
        model.put("images",images);
        return "/front/myUpload.html";
    }

}
